package com.sixmac.utils;

import com.sixmac.entity.MessageInfo;
import com.sixmac.entity.Placard;

import java.util.*;

/**
 * Created by Administrator on 2016/3/10.
 */
public class MessageTimerWork {

    private static Map<String, Object> map = new HashMap<String, Object>();

    private Timer timer = null;

    private MessageInfo messageInfo;

    private Placard placard;

    private Integer type;

    public MessageTimerWork(Date time, MessageInfo messageInfo, Placard placard, Integer type) {
        timer = new Timer();
        this.messageInfo = messageInfo;
        this.placard = placard;
        this.type = type;
        timer.schedule(new MyWork(), time);

        if (type == 1) {
            map.put("one" + messageInfo.getId(), timer);
        } else {
            map.put("two" + placard.getId(), timer);
        }
    }

    class MyWork extends TimerTask {
        @Override
        public void run() {
            try {
                doExecute();
                timer.cancel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void doExecute() {
        System.out.println("===============发送消息============");
        try {
            if (type == 1) {
                // 系统消息
                System.out.println("***********************************************发送系统消息***************************************************");
            } else {
                // 公告信息
                System.out.println("***********************************************发送公告信息***************************************************");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void killWork(String key) {
        Timer timer1 = (Timer) map.get(key);
        if (null != timer1) {
            timer1.cancel();
        }
    }
}
